package com.kitwee.serialport.data.model;


import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

@Entity(
        tableName = "serial_port_data",
        indices = {@Index(value = {"flag"}, unique = true)}
)
public class SerialPortData implements Parcelable {

    @NonNull
    @PrimaryKey
    @ColumnInfo(name = "flag")
    private String flag;

    private String data;

    @Ignore
    private boolean reported = true;

    public SerialPortData() {
    }

    @Ignore
    public SerialPortData(String flag, String data) {
        this.flag = flag;
        this.data = data;
    }

    protected SerialPortData(Parcel in) {
        flag = in.readString();
        data = in.readString();
    }

    public static final Creator<SerialPortData> CREATOR = new Creator<SerialPortData>() {
        @Override
        public SerialPortData createFromParcel(Parcel in) {
            return new SerialPortData(in);
        }

        @Override
        public SerialPortData[] newArray(int size) {
            return new SerialPortData[size];
        }
    };

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(flag);
        dest.writeString(data);
    }

    public boolean isReported() {
        return reported;
    }

    public void setReported(boolean reported) {
        this.reported = reported;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        SerialPortData that = (SerialPortData) obj;
        return flag.equals(that.flag);
    }


    @Override
    public int hashCode() {
        return 31 * flag.hashCode();
    }

}
